# -*- coding: utf-8 -*-
"""
@Time    : 2025/1/16 11:45 
@Author  : ZhangShenao 
@File    : 1.OpenAI流式响应.py 
@Desc    : OpenAI流式响应
"""
import os

import dotenv
from openai import OpenAI

dotenv.load_dotenv()
client = OpenAI(
    base_url=os.getenv("OPENAI_API_BASE")
)

completion = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "developer", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ],
    stream=True
)

# 流式响应
# id: 所有消息块的 ID 是一样的，保证它们是一个消息
# object: 消息类型是 chat.completion.chunk，这说明它是一个消息块
for chunk in completion:
    print(chunk)
